package com.smart.community.region.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 区域统计实体类
 * 
 * @author Wu.Liang
 * @since 2024-12-21
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sc_region_stats")
public class RegionStats implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 统计ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 区域ID
     */
    @TableField("region_id")
    private Long regionId;

    /**
     * 统计日期
     */
    @TableField("stat_date")
    private LocalDate statDate;

    /**
     * 社区数量
     */
    @TableField("community_count")
    private Integer communityCount;

    /**
     * 楼栋数量
     */
    @TableField("building_count")
    private Integer buildingCount;

    /**
     * 房户数量
     */
    @TableField("household_count")
    private Integer householdCount;

    /**
     * 居民数量
     */
    @TableField("resident_count")
    private Integer residentCount;

    /**
     * 总面积（平方米）
     */
    @TableField("area_total")
    private BigDecimal areaTotal;

    /**
     * 入住率（%）
     */
    @TableField("occupancy_rate")
    private BigDecimal occupancyRate;

    /**
     * 创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    // ==================== 业务方法 ====================

    /**
     * 计算户均面积
     */
    @JsonIgnore
    public BigDecimal getAverageAreaPerHousehold() {
        if (areaTotal == null) {
            return BigDecimal.ZERO;
        }
        if (householdCount == null || householdCount == 0) {
            return BigDecimal.ZERO;
        }
        return areaTotal.divide(BigDecimal.valueOf(householdCount), 2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 计算人均面积
     */
    @JsonIgnore
    public BigDecimal getAverageAreaPerPerson() {
        if (areaTotal == null) {
            return BigDecimal.ZERO;
        }
        if (residentCount == null || residentCount == 0) {
            return BigDecimal.ZERO;
        }
        return areaTotal.divide(BigDecimal.valueOf(residentCount), 2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 计算户均人数
     */
    @JsonIgnore
    public BigDecimal getAveragePersonPerHousehold() {
        if (residentCount == null) {
            return BigDecimal.ZERO;
        }
        if (householdCount == null || householdCount == 0) {
            return BigDecimal.ZERO;
        }
        return BigDecimal.valueOf(residentCount)
                .divide(BigDecimal.valueOf(householdCount), 2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 计算楼栋密度
     */
    @JsonIgnore
    public BigDecimal getBuildingDensity() {
        if (buildingCount == null) {
            return BigDecimal.ZERO;
        }
        if (areaTotal == null || areaTotal.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        }
        // 每平方公里楼栋数
        return BigDecimal.valueOf(buildingCount)
                .divide(areaTotal.divide(BigDecimal.valueOf(1000000), 6, BigDecimal.ROUND_HALF_UP), 2, BigDecimal.ROUND_HALF_UP);
    }
} 